package com.atguigu.day03;

import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink11_Transform_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 3.使用process将每一行数据按照空格切分切出每一个单词，然后组成Tuple2元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneDStream = streamSource.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {


                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        //4.将相同的单词聚合到一块
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordToOneDStream.keyBy(0);

        //TODO 5.使用procee实现sum的功能
        keyedStream.process(new KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            //定义一个累加器
//            private Integer count = 0;

            //定义一个kv类型的累加器
            private  HashMap<String, Integer> map = new HashMap<>();

            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                //1.当单词是第一次来的时候，先存
                if (!map.containsKey(value.f0)){
                    //当前单词的累加器不存在,则将自己的个数存起来
                    map.put(value.f0, value.f1);
                }else {
                    //map中已经有这个单词的累加器了,则更新累加器结果
                    //首先获取之前保存的结果
                    Integer lastSum = map.get(value.f0);
                    //更新累加结果
                    lastSum += value.f1;
                    //把结果重新写入map中
                    map.put(value.f0, lastSum);
                }
                out.collect(Tuple2.of(value.f0,map.get(value.f0)));
            }
        }).print();

        env.execute();
    }
}
